
#include "DIKeyboard.h"

static UINT __Scan2Ascii(unsigned char* pKeyboardState,unsigned char ScanCode,unsigned short*pBuff)
{
	UINT vk = MapVirtualKey(ScanCode,1);
	int n = ToAscii(vk,(UINT)ScanCode,pKeyboardState,pBuff,0);
	return n;
}

UINT CDIKeyboard::ReadCharInput( unsigned short* pBuff, BOOL*pKeystate, UINT nWords )
{
	UINT nCount = 0;

	unsigned char Win32KeyboardState[256];
	GetKeyboardState(Win32KeyboardState);
	unsigned short CharBuff[2];
	UINT nWords1 = 0,nWords2 = 0;


	for( int i = 0;i<MAX_KEYSTATE;i++)
	{
		if(ButtonPressed(m_State[i]))
		{
			nWords1 = __Scan2Ascii(Win32KeyboardState,i,CharBuff);

			if(nWords1 == 0)
			{
				if(Win32KeyboardState[VK_NUMLOCK]&0x01)
				{			
					if(Win32KeyboardState[VK_NUMPAD0]&0x80)
					{	CharBuff[0] = '0';nWords1 = 1;}
					else if(Win32KeyboardState[VK_NUMPAD1]&0x80)
					{	CharBuff[0] = '1';nWords1 = 1;}
					else if(Win32KeyboardState[VK_NUMPAD2]&0x80)
					{	CharBuff[0] = '2';nWords1 = 1;}
					else if(Win32KeyboardState[VK_NUMPAD3]&0x80)
					{	CharBuff[0] = '3';nWords1 = 1;}
					else if(Win32KeyboardState[VK_NUMPAD4]&0x80)
					{	CharBuff[0] = '4';nWords1 = 1;}
					else if(Win32KeyboardState[VK_NUMPAD5]&0x80)
					{	CharBuff[0] = '5';;nWords1 = 1;}
					else if(Win32KeyboardState[VK_NUMPAD6]&0x80)
					{	CharBuff[0] = '6';;nWords1 = 1;}
					else if(Win32KeyboardState[VK_NUMPAD7]&0x80)
					{	CharBuff[0] = '7';;nWords1 = 1;}
					else if(Win32KeyboardState[VK_NUMPAD8]&0x80)
					{	CharBuff[0] = '8';;nWords1 = 1;}
					else if(Win32KeyboardState[VK_NUMPAD9]&0x80)
					{	CharBuff[0] = '9';;nWords1 = 1;}
					else if(Win32KeyboardState[VK_DECIMAL]&0x80)
					{	CharBuff[0] = '.';;nWords1 = 1;}
					else if(Win32KeyboardState[VK_DIVIDE]&0x80)
					{	CharBuff[0] = '/';;nWords1 = 1;}			
				}
			}

			if(nWords1 == 0)
				continue;

			if(nCount + nWords1 > nWords)
				break;

			for(unsigned int j = 0;j< nWords1;j++)
			{
				pBuff[nWords2+j] = CharBuff[j];
			}

			nWords2 += nWords1;

			UINT stat = m_State[i];
			UINT stat_last = m_StateLast[i];

			if( ButtonDown(stat,stat_last))
			{
				pKeystate[nCount] = true;
			}
			else
			{
				pKeystate[nCount] = false;
			}


			nCount++;
		}
	}


	return nCount;
}


CDIKeyboard::CDIKeyboard()
{
	memset( m_State,0,MAX_KEYSTATE );
	memset( m_StateLast,0,MAX_KEYSTATE );
}

CDIKeyboard::~CDIKeyboard()
{
}

bool CDIKeyboard::Init( HWND hWnd )
{
	m_pDevice->SetDataFormat( &c_dfDIKeyboard );
	m_pDevice->SetCooperativeLevel( hWnd, DISCL_FOREGROUND|DISCL_NONEXCLUSIVE );
	m_pDevice->Acquire();

	m_hWnd = hWnd;

	return true;
}

bool CDIKeyboard::Update()
{
	HRESULT hr = m_pDevice->Acquire();

	if(FAILED(hr))
	{
		return false;
	}
	else
	{
		memcpy( m_StateLast,m_State,MAX_KEYSTATE );
		m_pDevice->GetDeviceState( MAX_KEYSTATE,m_State);
	}

	return true;
}

bool CDIKeyboard::IsKeyPressed( DWORD dik )
{
	int keystat = m_State[dik];
	return ButtonPressed( keystat )?true:false;
}

bool CDIKeyboard::IsKeyDown( DWORD dik )
{
	int keystat = m_State[dik] & 0x80;
	int keystat_last = m_StateLast[dik]  & 0x80;
	
	return ButtonDown( keystat,keystat_last )?true:false;
}

bool CDIKeyboard::IsKeyUp( DWORD dik )
{
	int keystat = m_State[dik]  & 0x80 ;
	int keystat_last = m_StateLast[dik]  & 0x80;
	
	return ButtonUp( keystat,keystat_last )?true:false;
}
